package com.hejia.bulbul.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.hejia.alauda.entity.Parameter;
import com.hejia.alauda.service.ConfigureService;

@Controller
@RequestMapping("setting/announcement")
public class SettingAnnouncementController extends BaseController {

	@Resource
	private ConfigureService parameterService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(ModelMap model) {
		List<Parameter> list = parameterService
				.getParameters(Parameter.Category.Announcement);
		List<Map<String, String>> params = new ArrayList<Map<String, String>>();
		for (int i = 0; i < list.size(); i++) {
			Parameter parameter = list.get(i);
			Map<String, String> map = new HashMap<String, String>();
			JSONObject json = JSONObject.parseObject(parameter.getValue());
			map.put("id", parameter.getId().toString());
			map.put("title", json.getString("title"));
			map.put("link", json.getString("link"));
			map.put("date", json.getString("date"));
			map.put("type", json.getString("type"));

			params.add(map);
		}
		model.put("list", list);
		model.put("params", params);
		return "/setting/announcementList";
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addAnnouncement(ModelMap model) {

		return "/setting/announcementEdit";
	}

	/**
	 * 添加完保存
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String saveAnnouncement(String type, String title, String link,
			String date, ModelMap model, RedirectAttributes redirectAttributes) {
		Parameter parameter = new Parameter();
		parameter.setCreateDate(new Date());
		parameter.setModifyDate(new Date());

		JSONObject json = new JSONObject();
		json.put("title", title);
		json.put("link", link);
		json.put("date", date);
		json.put("type", type);
		parameter.setCode(type);
		parameter.setValue(json.toJSONString());
		parameter.setCategory(Parameter.Category.Announcement);
		if (parameterService.saveParameter(parameter)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "公告添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "公告添加失败");
		}

		return "redirect:/setting/announcement/list";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateAnnouncement(@PathVariable Long id, ModelMap model) {
		Parameter parameter = parameterService.getParameterById(id);
		
		Map<String, String> map = new HashMap<String, String>();
		JSONObject json = JSONObject.parseObject(parameter.getValue());
		map.put("id", parameter.getId().toString());
		map.put("title", json.getString("title"));
		map.put("link", json.getString("link"));
		map.put("date", json.getString("date"));
		map.put("type", json.getString("type"));

		model.put("parameter", map);

		return "/setting/announcementEdit";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
	public String saveUpdateAnnouncement(@PathVariable Long id, String type,
			String title, String link, String date, ModelMap model,
			RedirectAttributes redirectAttributes) {
		Parameter parameter2 = parameterService.getParameterById(id);

		JSONObject json = new JSONObject();
		json.put("title", title);
		json.put("link", link);
		json.put("date", date);
		json.put("type", type);
		parameter2.setCode(type);
		parameter2.setValue(json.toJSONString());
		parameter2.setModifyDate(new Date());

		if (parameterService.updateParameter(parameter2)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "公告修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "公告修改失败");
		}

		return "redirect:/setting/announcement/list";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String deleteAnnouncement(@PathVariable Long id, ModelMap model,
			RedirectAttributes redirectAttributes) {
		if (parameterService.deleteParameter(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "公告删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "公告删除失败");
		}
		return "redirect:/setting/announcement/list";
	}
}
